﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace DataAccess_Layer
{
    public static class LichSuMuaHang_DAC
    {
        private static DataClasses_RetailerDataContext db = DataAccessHelper.DB;
        public static void insert(LichSuMuaHang cat)
        {
            db.LichSuMuaHangs.InsertOnSubmit(cat);
            DataAccessHelper.Save();
        }

        public static void delete(List<long> deletedIDs)
        {
            foreach (long deletedID in deletedIDs)
            {
                LichSuMuaHang nccgh = DataAccessHelper.DB.LichSuMuaHangs.Where(ncc => ncc.ID == deletedID).Select(ncc => ncc).First();
                DataAccessHelper.DB.LichSuMuaHangs.DeleteOnSubmit(nccgh);
            }
            DataAccessHelper.Save();
        }

        public static void update(LichSuMuaHang nCC_GiangHang)
        {
            LichSuMuaHang ctls = DataAccessHelper.DB.LichSuMuaHangs.Where(ncc => ncc.ID == nCC_GiangHang.ID).Select(ncc => ncc).First();
            ctls.ID = nCC_GiangHang.ID;
            ctls.NgayLapHoaDon = nCC_GiangHang.NgayLapHoaDon;
            ctls.TongTien = nCC_GiangHang.TongTien;
            ctls.Customer = Customer_DAC.lay(nCC_GiangHang.ID_Customer.Value);
            DataAccessHelper.Save();
        }

        public static object layHet()
        {
            return DataAccessHelper.DB.LichSuMuaHangs.Select(cat => new { cat.ID,cat.NgayLapHoaDon, cat.TongTien, cat.ID_Customer,TenKhachHang = cat.Customer.UserName });
        }
       
        public static object timKiem_LSMH(DateTime? tuNgay, DateTime? denNgay, long? giaTu, long? giaDen)
        {
            var lichSus = DataAccessHelper.DB.LichSuMuaHangs.Select(cat => new { cat.ID, cat.NgayLapHoaDon, cat.TongTien, cat.ID_Customer, TenKhachHang = cat.Customer.UserName });
            List<DTO.LichSuMuaHangMoRong_DTO> rs = new List<DTO.LichSuMuaHangMoRong_DTO>();
            // Cong viec tiep theo: debug hien trang , kt xem chay dung chua                       

            //lọc buớc 1: kt các dieu kien gia tu, gia den     
            if (giaTu != null && giaDen != null)
            {
                lichSus = DataAccessHelper.DB.LichSuMuaHangs.Where(pp => Convert.ToInt64(pp.TongTien) >= giaTu && Convert.ToInt64(pp.TongTien) <= giaDen).Select(pp => new { pp.ID, pp.NgayLapHoaDon, pp.TongTien, pp.ID_Customer, TenKhachHang = pp.Customer.UserName });
                foreach (var ls in lichSus)
                {
                    DateTime ngayLapHD = DateTime.Parse(ls.NgayLapHoaDon, new System.Globalization.CultureInfo("fr-FR", true));
                    if (tuNgay != null && denNgay != null && ngayLapHD.CompareTo(tuNgay) >= 0 && ngayLapHD.CompareTo(denNgay) <= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay == null && denNgay != null && ngayLapHD.CompareTo(denNgay) <= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay != null && denNgay == null && ngayLapHD.CompareTo(tuNgay) >= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay == null && denNgay == null)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                }
            } 
            else if (giaTu != null && giaDen == null)
            {
                lichSus = DataAccessHelper.DB.LichSuMuaHangs.Where(pp => Convert.ToInt64(pp.TongTien) >= giaTu).Select(pp => new { pp.ID, pp.NgayLapHoaDon, pp.TongTien, pp.ID_Customer, TenKhachHang = pp.Customer.UserName });
                foreach (var ls in lichSus)
                {
                    DateTime ngayLapHD = DateTime.Parse(ls.NgayLapHoaDon, new System.Globalization.CultureInfo("fr-FR", true));
                    if (tuNgay != null && denNgay != null && ngayLapHD.CompareTo(tuNgay) >= 0 && ngayLapHD.CompareTo(denNgay) <= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay == null && denNgay != null && ngayLapHD.CompareTo(denNgay) <= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay != null && denNgay == null && ngayLapHD.CompareTo(tuNgay) >= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay == null && denNgay == null)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                }
            }
            else if (giaTu == null && giaDen != null)
            {
                lichSus = DataAccessHelper.DB.LichSuMuaHangs.Where(pp => Convert.ToInt64(pp.TongTien) <= giaDen).Select(pp => new { pp.ID, pp.NgayLapHoaDon, pp.TongTien, pp.ID_Customer, TenKhachHang = pp.Customer.UserName });
                foreach (var ls in lichSus)
                {
                    DateTime ngayLapHD = DateTime.Parse(ls.NgayLapHoaDon, new System.Globalization.CultureInfo("fr-FR", true));
                    if (tuNgay != null && denNgay != null && ngayLapHD.CompareTo(tuNgay) >= 0 && ngayLapHD.CompareTo(denNgay) <= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay == null && denNgay != null && ngayLapHD.CompareTo(denNgay) <= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay != null && denNgay == null && ngayLapHD.CompareTo(tuNgay) >= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay == null && denNgay == null)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                }
            }
            else if (giaTu == null && giaDen == null)
            {
                foreach (var ls in lichSus)
                {
                    DateTime ngayLapHD = DateTime.Parse(ls.NgayLapHoaDon, new System.Globalization.CultureInfo("fr-FR", true));
                    if (tuNgay != null && denNgay != null && ngayLapHD.CompareTo(tuNgay) >= 0 && ngayLapHD.CompareTo(denNgay) <= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay == null && denNgay != null && ngayLapHD.CompareTo(denNgay) <= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay != null && denNgay == null && ngayLapHD.CompareTo(tuNgay) >= 0)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                    else if (tuNgay == null && denNgay == null)
                    {
                        DTO.LichSuMuaHangMoRong_DTO lsmh = new DTO.LichSuMuaHangMoRong_DTO();
                        lsmh.ID = ls.ID;
                        lsmh.ID_Customer = ls.ID_Customer.Value;
                        lsmh.NgayLapHoaDon = ls.NgayLapHoaDon;
                        lsmh.TenKhachHang = ls.TenKhachHang;
                        lsmh.TongTien = ls.TongTien;

                        rs.Add(lsmh);
                    }
                }
            }

            return rs;          
        }

        public static LichSuMuaHang lay(int id)
        {
            return DataAccessHelper.DB.LichSuMuaHangs.Where(cat=>cat.ID == id).Select(cat => cat).First();
        }

        public static string layNewID()
        {
            return DataAccessHelper.DB.LichSuMuaHangs.Max(pp => pp.ID).ToString();
        }
    }
}
